package com.shiningstars.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

import com.shiningstars.R;
import com.shiningstars.general.Config;
import com.shiningstars.general.Constants;
import com.shiningstars.general.Messages;
import com.shiningstars.general.Utilities;
import com.shiningstars.tools.ExceptionHandler;
import com.shiningstars.valuebeans.Feedback;

public class TeacherFeedback4Activity extends Activity implements Constants, Messages, Config {

	Feedback feedback;

	RadioGroup radioGroupMaterialReady;
	RadioButton radioMaterialReadyYes;
	RadioButton radioMaterialReadyNo;
	EditText editTextMaterialRequired;
	EditText editTextActivityTime;
	RadioGroup radioGroupActivityCleaning;
	RadioButton radioActivityCleaningYes;
	RadioButton radioActivityCleaningNo;
	TextView textViewActivityResponse;
	RatingBar ratingBarActivityResponse;
	EditText editTextActivityComments;

	RadioGroup radioGroupGame;
	RadioButton radioGameJainismYes;
	RadioButton radioGameJainismNo;
	TextView textViewGameResponse;
	RatingBar ratingBarGameResponse;
	EditText editTextGameComments;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_teacher_feedback_4);

			Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
			SplashPageActivity.showSplash = false;

			getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

			radioGroupMaterialReady = (RadioGroup) findViewById(R.id.tf4RadioGroup1);
			radioMaterialReadyYes = (RadioButton) findViewById(R.id.tf4RadioButton1);
			radioMaterialReadyNo = (RadioButton) findViewById(R.id.tf4RadioButton2);
			editTextMaterialRequired = (EditText) findViewById(R.id.tf4EditText1);
			editTextActivityTime = (EditText) findViewById(R.id.tf4EditText2);
			radioGroupActivityCleaning = (RadioGroup) findViewById(R.id.tf4RadioGroup2);
			radioActivityCleaningYes = (RadioButton) findViewById(R.id.tf4RadioButton3);
			radioActivityCleaningNo = (RadioButton) findViewById(R.id.tf4RadioButton4);
			textViewActivityResponse = (TextView) findViewById(R.id.tf4TextView1);
			ratingBarActivityResponse = (RatingBar) findViewById(R.id.tf4RatingBar1);
			editTextActivityComments = (EditText) findViewById(R.id.tf4EditText3);

			radioGroupGame = (RadioGroup) findViewById(R.id.tf4RadioGroup3);
			radioGameJainismYes = (RadioButton) findViewById(R.id.tf4RadioButton5);
			radioGameJainismNo = (RadioButton) findViewById(R.id.tf4RadioButton6);
			textViewGameResponse = (TextView) findViewById(R.id.tf4TextView2);
			ratingBarGameResponse = (RatingBar) findViewById(R.id.tf4RatingBar2);
			editTextGameComments = (EditText) findViewById(R.id.tf4EditText4);

			ratingBarActivityResponse.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

				@Override
				public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
					Utilities.setRatingDescription(textViewActivityResponse, rating);
				}
			});

			ratingBarGameResponse.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

				@Override
				public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
					Utilities.setRatingDescription(textViewGameResponse, rating);
				}
			});

			if (null != getIntent()) {
				if (null != getIntent().getExtras()) {
					if (null != getIntent().getExtras().get(FEEDBACK)) {
						feedback = (Feedback) getIntent().getExtras().get(FEEDBACK);

						if (YES.equals(feedback.getQuestion37())) {
							radioMaterialReadyYes.setChecked(true);
						} else if (NO.equals(feedback.getQuestion37())) {
							radioMaterialReadyNo.setChecked(true);
						}

						editTextMaterialRequired.setText(feedback.getQuestion38());
						editTextActivityTime.setText(feedback.getQuestion39());

						if (YES.equals(feedback.getQuestion40())) {
							radioActivityCleaningYes.setChecked(true);
						} else if (NO.equals(feedback.getQuestion40())) {
							radioActivityCleaningNo.setChecked(true);
						}

						if (!Utilities.isBlankString(feedback.getQuestion41())) {
							float rating = Float.parseFloat(feedback.getQuestion41());
							ratingBarActivityResponse.setRating(rating);
						}

						editTextActivityComments.setText(feedback.getQuestion42());

						if (YES.equals(feedback.getQuestion43())) {
							radioGameJainismYes.setChecked(true);
						} else if (NO.equals(feedback.getQuestion43())) {
							radioGameJainismNo.setChecked(true);
						}

						if (!Utilities.isBlankString(feedback.getQuestion44())) {
							float rating = Float.parseFloat(feedback.getQuestion44());
							ratingBarGameResponse.setRating(rating);
						}

						editTextGameComments.setText(feedback.getQuestion45());
					}
				}
			}
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), TeacherFeedback4Activity.class.getName(), e);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		try {
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.feedback_menu, menu);
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), TeacherFeedback4Activity.class.getName(), e);
		}

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		try {
			if (validate()) {
				Intent i = null;

				if (BACK.equals(item.getTitle())) {
					i = new Intent(TeacherFeedback4Activity.this, TeacherFeedback3Activity.class);
				} else if (NEXT.equals(item.getTitle())) {
					i = new Intent(TeacherFeedback4Activity.this, TeacherFeedback5Activity.class);
				}

				i.putExtra(FEEDBACK, feedback);
				startActivity(i);
				finish();
			}
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), TeacherFeedback4Activity.class.getName(), e);
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		try {
			AlertDialog.Builder builder = new AlertDialog.Builder(TeacherFeedback4Activity.this);

			builder.setMessage(FEEDBACK_CANCEL);
			builder.setCancelable(false);
			builder.setPositiveButton(OK, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					Intent i = null;

					String currentRole = Utilities.getSharedPreference(getApplicationContext(), CURRENT_ROLE);
					if (ADMIN.equals(currentRole)) {
						i = new Intent(TeacherFeedback4Activity.this, AdminActivity.class);
					} else if (TEACHER.equals(currentRole)) {
						i = new Intent(TeacherFeedback4Activity.this, MyFeedbackActivity.class);
					}

					startActivity(i);
					finish();
				}
			});
			builder.setNegativeButton(CANCEL, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {

				}
			});

			AlertDialog alert = builder.create();
			alert.show();
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), TeacherFeedback4Activity.class.getName(), e);
		}
	}

	private boolean validate() {
		boolean isValid = true;
		String errorMessage = null;

		int radioGroupMaterialReadyId = radioGroupMaterialReady.getCheckedRadioButtonId();
		if (isValid && radioGroupMaterialReadyId == -1) {
			isValid = false;
			errorMessage = INVALID_MATERIAL_READY;
			radioGroupMaterialReady.requestFocus();
		} else {
			if (radioGroupMaterialReadyId == radioMaterialReadyYes.getId()) {
				feedback.setQuestion37(radioMaterialReadyYes.getText().toString());
			} else if (radioGroupMaterialReadyId == radioMaterialReadyNo.getId()) {
				feedback.setQuestion37(radioMaterialReadyNo.getText().toString());
			}
		}

		if (isValid && Utilities.isBlankString(editTextMaterialRequired.getText().toString())) {
			isValid = false;
			errorMessage = INVALID_MATERIAL;
			editTextMaterialRequired.requestFocus();
		} else {
			feedback.setQuestion38(editTextMaterialRequired.getText().toString());
		}

		if (isValid && Utilities.isBlankString(editTextActivityTime.getText().toString())) {
			isValid = false;
			errorMessage = INVALID_ACTIVITY_TIME;
			editTextActivityTime.requestFocus();
		} else {
			feedback.setQuestion39(editTextActivityTime.getText().toString());
		}

		int radioGroupActivityCleaningId = radioGroupActivityCleaning.getCheckedRadioButtonId();
		if (isValid && radioGroupActivityCleaningId == -1) {
			isValid = false;
			errorMessage = INVALID_ACTIVITY_CLEANING;
			radioGroupActivityCleaning.requestFocus();
		} else {
			if (radioGroupActivityCleaningId == radioActivityCleaningYes.getId()) {
				feedback.setQuestion40(radioActivityCleaningYes.getText().toString());
			} else if (radioGroupActivityCleaningId == radioActivityCleaningNo.getId()) {
				feedback.setQuestion40(radioActivityCleaningNo.getText().toString());
			}
		}

		if (isValid && ratingBarActivityResponse.getRating() == 0) {
			isValid = false;
			errorMessage = INVALID_ACTIVITY_RESPONSE;
			ratingBarActivityResponse.requestFocus();
		} else {
			feedback.setQuestion41(String.valueOf(ratingBarActivityResponse.getRating()));
		}

		if (isValid && Utilities.isBlankString(editTextActivityComments.getText().toString())) {
			isValid = false;
			errorMessage = INVALID_ACTIVITY_COMMENTS;
			editTextActivityComments.requestFocus();
		} else {
			feedback.setQuestion42(editTextActivityComments.getText().toString());
		}

		int radioGroupGameId = radioGroupGame.getCheckedRadioButtonId();
		if (isValid && radioGroupGameId == -1) {
			isValid = false;
			errorMessage = INVALID_JAINISM_GAME;
			radioGroupGame.requestFocus();
		} else {
			if (radioGroupGameId == radioGameJainismYes.getId()) {
				feedback.setQuestion43(radioGameJainismYes.getText().toString());
			} else if (radioGroupGameId == radioGameJainismNo.getId()) {
				feedback.setQuestion43(radioGameJainismNo.getText().toString());
			}
		}

		if (isValid && ratingBarGameResponse.getRating() == 0) {
			isValid = false;
			errorMessage = INVALID_GAME_RESPONSE;
			ratingBarGameResponse.requestFocus();
		} else {
			feedback.setQuestion44(String.valueOf(ratingBarGameResponse.getRating()));
		}

		if (isValid && Utilities.isBlankString(editTextGameComments.getText().toString())) {
			isValid = false;
			errorMessage = INVALID_GAME_COMMENTS;
			editTextGameComments.requestFocus();
		} else {
			feedback.setQuestion45(editTextGameComments.getText().toString());
		}

		if (!isValid) {
			AlertDialog.Builder builder = new AlertDialog.Builder(TeacherFeedback4Activity.this);

			builder.setMessage(errorMessage);
			builder.setCancelable(false);
			builder.setTitle(ERROR);
			builder.setPositiveButton(OK, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
				}
			});

			AlertDialog alert = builder.create();
			alert.show();
		}

		return isValid;
	}

	@Override
	protected void onPause() {
		super.onPause();
		SplashPageActivity.showSplash = true;
	}

	@Override
	protected void onResume() {
		super.onResume();

		if (SplashPageActivity.showSplash) {
			LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
			View popupView = layoutInflater.inflate(R.layout.activity_splash_page, null);
			final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT, true);

			findViewById(R.id.tf4EditText1).post(new Runnable() {
				public void run() {
					popupWindow.showAtLocation(findViewById(R.id.tf4EditText1), Gravity.CENTER, 0, 0);
				}
			});

			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					if (null != popupWindow && popupWindow.isShowing()) {
						popupWindow.dismiss();
					}
				}
			}, SPLASH_TIME_OUT);

			SplashPageActivity.showSplash = false;
		}
	}
}